Added CompressionGzipMiddleware for compressed responses#219
Added CompressionGzipMiddleware for compressed responses#219christoph-kluge wants to merge 12 commits intoreactphp:masterfrom
Conversation
README.md
Outdated
|
|
||
| #### CompressionGzipMiddleware | ||
|
|
||
| One of the built-in middleware is the `RequestBodyBufferMiddleware` which |
There was a problem hiding this comment.
RequestBodyBufferMiddleware -> CompressionGzipMiddleware
README.md
Outdated
| Usage: | ||
|
|
||
| ```php | ||
| $middlewares = new MiddlewareRunner([ |
There was a problem hiding this comment.
We try to avoid middlewares (plural) since strictly grammatically, there is no plural form of middleware (just like software).
README.md
Outdated
|
|
||
| ```php | ||
| $middlewares = new MiddlewareRunner([ | ||
| new \React\Http\Middleware\CompressionGzipMiddleware(), |
There was a problem hiding this comment.
No need to use the FQCN here, just CompressionGzipMiddleware (like you do with MiddlewareRunner 😃 ).
| $next = $this->getNextCallback($request, $response); | ||
| $middleware = new CompressionGzipMiddleware(); | ||
|
|
||
| /** @var FulfilledPromise $result */ |
There was a problem hiding this comment.
FulfilledPromise -> PromiseInterface
|
|
||
| /** @var FulfilledPromise $result */ | ||
| $result = $middleware($request, $next); | ||
| $result->done(function ($value) use (&$response) { |
There was a problem hiding this comment.
You can't use done() here as we're supporting react/promise 1.x. Same for other methods below.
…ect middleware name
…e with namespaces
|
Well.. tests are fine now but I realised that i.e. chrome was not able to "decode" the data. I'm fighting right now to support php5.3+ with different encoding methods. By now this code does not work properly. I'm working on a solution.. |
|
Couple of commits and some changes added but finally I think we got a working example across all versions 🎉 Looking forward for your reviews! |
|
What's missing is a detection if the response is actually compressible. A simple check would to test the Content-Type header of the response for |
|
@jsor very good point 👍 After some research I found out there's 2 approaches around the web servers..
Which way do we want to go? To be open for approaches I would suggest to inject the logic through an interface.. Interface + ClassUsage example#1 with auto-detectionUsage example#2 with manual definition |
|
@christoph-kluge Thank you for filing this PR, much appreciated! 👍 I really like seeing these new middleware handlers pop up! Given the current discussion in #221 it looks like this middleware handler may be better off in a separate package instead of being part of react/http. I agree that this middleware handler may use a number of different strategies to decide which content should be compressed as per your above discussion. I feel that these strategies are beyond the scope of this package at the moment, so I would much rather have all the relevant strategies available somewhere and then prominently link to this instead. What do you think about this? |
| return $response; | ||
| } | ||
|
|
||
| $content = $response->getBody()->getContents(); |
There was a problem hiding this comment.
This will cast the response body to a string. In case, the body is a HttpBodyStream, that doesn't work.
You probably need to decorate the stream and compress on the fly, eg. using https://github.com/clue/php-zlib-react.
|
I also think this middleware might be better suited for a dedicated package since the discussion has shown, that it will grow beyond a single simple middleware class. That way, it can define its own interfaces and dependencies, eg. for the platform requirement if you want to drop support for PHP 5.3. |
|
@jsor @christoph-kluge @clue what do you think of putting this under the https://github.com/friends-of-reactphp umbrella? |
|
@WyriHaximus @jsor @clue after the discussion in #221 and re-reading the rfc2616 (3.5 Content Codings) I would expect that we have a compression middleware here for handling the basic reading and writing of the headers and allow custom tokens through an So we could end up with http-support here and add custom tokens like what do you think? |
|
Since we're currently focussing on ironing out the core middleware system, i propose the following steps:
What do you think? |
|
@jsor sound very reasonable to me. Will ping you as soon as it's ready :) |
|
@christoph-kluge please ping me as well, would love to take it for a test and provide feedback 👍 |
As of the discussion in #48 and the ability to have middleware's I added a gzip compression middleware in order to support compressed responses.